﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyVoteMVC.Web.Models;
using MyVoteMVC.Domain;

namespace MyVoteMVC.Web.Extensions
{
    public static class DomainExtension  
    {
        /// <summary>
        /// 实现界面类和实体类之间的互相转换
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="entityList"></param>
        /// <param name="converter">转换接口</param>
        /// <returns></returns>
        public static IList<TModel> ToModelList<TEntity, TModel>(this IEnumerable<TEntity> entityList, IModelConverter<TEntity, TModel> converter)
            where TEntity : class
            where TModel : class
        {
            if (converter == null)
            {
                throw new ArgumentNullException("必须传入一个实现了IModelConverter的实例");
            }

            IList<TModel> modelList = new List<TModel>();
            foreach (TEntity entity in entityList)
            {
                modelList.Add(converter.Convert(entity));
            }

            return modelList;
        }

        //public static IList<TEntity> ToEntityList<TEntity, TModel>(this IEnumerable<TModel> modelList, IModelConverter<TEntity, TModel> converter)
        //    where TEntity : class
        //    where TModel : class
        //{
        //    if (converter == null)
        //    {
        //        throw new ArgumentNullException("必须传入一个实现了IModelConverter的实例");
        //    }

        //    IList<TEntity> entityList = new List<TEntity>();
        //    foreach (TModel model in modelList)
        //    {
        //        entityList.Add(converter.ReverseConvert(model));
        //    }

        //    return entityList;
        //}
    }

    #region Converter

    public class ActivityConverter : IModelConverter<VT_Activities, Activity>
    {
        public Activity Convert(VT_Activities entity)
        {
            Activity model = new Activity();
            model.Content = entity.Activity_Content;
            model.StartTime = entity.Activity_StartTime;
            model.EndTime = entity.Activity_EndTime;
            model.ID = entity.Activity_ID;
            //model.IfMultiSubmit = entity.Activity_IfMultiSubmit;
            model.IfReadAll = entity.Activity_IfReadAll;
            //model.IfShowUser = entity.Activity_IfShowUser;
            //model.Memo = entity.Memo;
            //model.No = entity.Activity_No;
            model.PID = entity.Activity_PID;
            model.Title = entity.Activity_Title;
            model.View_Count = entity.View_Count.HasValue?entity.View_Count.Value:0;
            model.Ticket_Count = entity.Tickets.Count;
            //model.Image = new ActivityImage()
            //{
            //    ID = entity.Activity_ID,
            //    Image = entity.ActivityImage.Activity_Image
            //};

            //model.Created_Action = entity.Created_Action;
            //model.Created_By = entity.Created_By;
            //model.Created_Date = entity.Created_Date;

            //model.Last_Update_Action = entity.Last_Update_Action;
            //model.Last_Update_By = entity.Last_Update_By;
            //model.Last_Update_Date = entity.Last_Update_Date;

            //MessageConverter messageConverter = new MessageConverter();
            //foreach (VT_Messages childEntity in entity.Messages)
            //    model.Messages.Add(messageConverter.Convert(childEntity));

            //QuestionConverter questionConverter = new QuestionConverter();
            //foreach (VT_Questions childEntity in entity.Questions)
            //    model.Questions.Add(questionConverter.Convert(childEntity));


            //TicketConverter ticketConverter = new TicketConverter();
            //foreach (VT_Tickets childEntity in entity.Tickets)
            //    model.Tickets.Add(ticketConverter.Convert(childEntity));

            //foreach (VT_Activities childEntity in entity.ChildActivities)
            //    model.ChildActivities.Add(Convert(childEntity));

            //就不向上获取了把？
            //model.ParentActivity = Convert(entity.ParentActivity);

            return model;
        }

        public void ReverseConvert(Activity model, VT_Activities entityToUpdated)
        {
            //只是更新当前实例了把。
            entityToUpdated.Activity_Content = model.Content;
            entityToUpdated.Activity_StartTime = model.StartTime;
            entityToUpdated.Activity_EndTime = model.EndTime;
            //entityToUpdated.Activity_IfMultiSubmit = model.IfMultiSubmit;
            entityToUpdated.Activity_IfReadAll = model.IfReadAll;
            //entityToUpdated.Activity_IfShowUser = model.IfShowUser;
            //entityToUpdated.Memo = model.Memo;
            //entityToUpdated.Activity_No = model.No;
            entityToUpdated.Activity_Title = model.Title;
            entityToUpdated.View_Count = model.View_Count;

            //entityToUpdated.ActivityImage = new VT_ActivityImage()
            //{
            //    Activity_ID = entityToUpdated.Activity_ID,
            //    Activity_Image = model.Image.Image
            //};

            //entityToUpdated.Created_Action = model.Created_Action;
            //entityToUpdated.Created_By = model.Created_By;
            //entityToUpdated.Created_Date = model.Created_Date;

            //entityToUpdated.Last_Update_Action = model.Last_Update_Action;
            //entityToUpdated.Last_Update_By = model.Last_Update_By;
            //entityToUpdated.Last_Update_Date = model.Last_Update_Date;

            //entityToUpdated.Activity_ID = model.ID;
            //entityToUpdated.Activity_PID = model.PID;

            //MessageConverter messageConverter = new MessageConverter();
            //foreach (Message childModel in model.Messages)
            //    entity.Messages.Add(messageConverter.ReverseConvert(childModel));

            //QuestionConverter questionConverter = new QuestionConverter();
            //foreach (Question childModel in model.Questions)
            //    entity.Questions.Add(questionConverter.ReverseConvert(childModel));


            //TicketConverter ticketConverter = new TicketConverter();
            //foreach (Ticket childModel in model.Tickets)
            //    entity.Tickets.Add(ticketConverter.ReverseConvert(childModel));

            //foreach (Activity childModel in model.ChildActivities)
            //    entity.ChildActivities.Add(ReverseConvert(childModel));

            //model.ParentActivity = Convert(entity.ParentActivity);
  
        }
    }

    public class MessageConverter : IModelConverter<VT_Messages, Message>
    {
        public Message Convert(VT_Messages entity)
        {
            Message model = new Message();
            model.Activity_ID = entity.Activity_ID;
            model.Content = entity.Message_Content;
            model.ID = entity.Message_ID;
            model.No = entity.Message_No;
            model.Title = entity.Message_Title;

            model.Created_Action = entity.Created_Action;
            model.Created_By = entity.Created_By;
            model.Created_Date = entity.Created_Date;

            model.Last_Update_Action = entity.Last_Update_Action;
            model.Last_Update_By = entity.Last_Update_By;
            model.Last_Update_Date = entity.Last_Update_Date;
            return model;
        }

        public void ReverseConvert(Message model, VT_Messages entityToUpdated)
        {
            entityToUpdated.Message_Content = model.Content;
            entityToUpdated.Message_No = model.No;
            entityToUpdated.Message_Title = model.Title;

            entityToUpdated.Created_Action = model.Created_Action;
            entityToUpdated.Created_By = model.Created_By;
            entityToUpdated.Created_Date = model.Created_Date;
            entityToUpdated.Last_Update_Action = model.Last_Update_Action;
            entityToUpdated.Last_Update_By = model.Last_Update_By;
            entityToUpdated.Last_Update_Date = model.Last_Update_Date;
        }
    }

    public class QuestionConverter : IModelConverter<VT_Questions, Question>
    {
        public Question Convert(VT_Questions entity)
        {
            Question model = new Question();
            model.Activity_ID = entity.Activity_ID;
            model.Content = entity.Question_Content;
            model.ID = entity.Question_ID;
            model.IfMultiItems = entity.Question_IfMultiItems;
            model.IfMustAnswer = entity.Question_IfMustAnswer;
            model.No = entity.Question_No;
            model.Status = entity.Question_Status;
            model.Title = entity.Question_Title;

            model.Created_Action = entity.Created_Action;
            model.Created_By = entity.Created_By;
            model.Created_Date = entity.Created_Date;

            model.Last_Update_Action = entity.Last_Update_Action;
            model.Last_Update_By = entity.Last_Update_By;
            model.Last_Update_Date = entity.Last_Update_Date;            

            //ItemConverter itemConverter = new ItemConverter();
            //foreach (VT_Items childEntity in entity.Items)
            //    model.Items.Add(itemConverter.Convert(childEntity));

            return model;
        }

        public void ReverseConvert(Question model, VT_Questions entityToUpdated)
        {
            entityToUpdated.Question_Title = model.Title;
            entityToUpdated.Question_Status = model.Status;
            entityToUpdated.Question_No = model.No;
            entityToUpdated.Question_IfMustAnswer = model.IfMustAnswer;
            entityToUpdated.Question_IfMultiItems = model.IfMultiItems;
            entityToUpdated.Question_Content = model.Content;

            entityToUpdated.Last_Update_Date = model.Last_Update_Date;
            entityToUpdated.Last_Update_By = model.Last_Update_By;
            entityToUpdated.Last_Update_Action = model.Last_Update_Action;

            entityToUpdated.Created_Date = model.Created_Date;
            entityToUpdated.Created_By = model.Created_By;
            entityToUpdated.Created_Action = model.Created_Action;
        }
    }

    public class ItemConverter : IModelConverter<VT_Items, Item>
    {
        public Item Convert(VT_Items entity)
        {
            Item model = new Item();
            model.Content = entity.Item_Content;
            model.ID = entity.Item_ID;
            model.IfDefault = entity.Item_IfDefault;
            //model.ItemImage = new ItemImage() {
            //    ID = entity.Item_ID,
            //    Image = entity.ItemImage.Item_Image 
            //};
            model.No = entity.Item_No;
            model.Offline_Count = entity.Offline_Count;
            model.Online_Count = entity.Online_Count;
            model.QID = entity.Question_ID;
            model.QuestionNo = entity.Question.Question_No.Value.ToString();
            model.QuestionTitle = entity.Question.Question_Title;
            model.QuestionContent = entity.Question.Question_Content;
            model.QuestionMulti = entity.Question.MultiCount;
            model.ActivityTitle = entity.Question.Activity.Activity_Title;
            model.ActivityContent = entity.Question.Activity.Activity_Content;

            model.Title = entity.Item_Title;

            //model.Created_Action = entity.Created_Action;
            //model.Created_By = entity.Created_By;
            //model.Created_Date = entity.Created_Date;

            //model.Last_Update_Action = entity.Last_Update_Action;
            //model.Last_Update_By = entity.Last_Update_By;
            //model.Last_Update_Date = entity.Last_Update_Date;
            

            if (entity.Project != null)
            {
                ProjectConverter projectConverter = new ProjectConverter();
                //model.Project = projectConverter.Convert(entity.Project);
                model.PID = entity.Project_ID;
                model.pName = entity.Project.Project_Name;
                model.pType = entity.Project.Project_Type;
                if (!string.IsNullOrEmpty(entity.Project.Project_Year))
                {
                    model.pYear = entity.Project.Project_Year;
                    model.pYearGroup = entity.Project.Project_Year.Substring(0, 3) + "0";
                }
                model.pAddress = entity.Project.Project_Address;
                model.pAwards = entity.Project.Project_Awards;
                model.pDescription = entity.Project.Project_Description;
            }

            return model;
        }

        public void ReverseConvert(Item model, VT_Items entityToUpdated)
        {
            entityToUpdated.Item_Content = model.Content;
            entityToUpdated.Item_IfDefault = model.IfDefault;
            entityToUpdated.Item_No = model.No;
            entityToUpdated.Item_Title = model.Title;
            //entityToUpdated.ItemImage = new VT_ItemImage()
            //{
            //    Item_ID = entityToUpdated.Item_ID,
            //    Item_Image = model.ItemImage.Image
            //};
            entityToUpdated.Offline_Count = model.Offline_Count;
            entityToUpdated.Online_Count = model.Online_Count;

            //entityToUpdated.Last_Update_Date = model.Last_Update_Date;
            //entityToUpdated.Last_Update_By = model.Last_Update_By;
            //entityToUpdated.Last_Update_Action = model.Last_Update_Action;

            //entityToUpdated.Created_Date = model.Created_Date;
            //entityToUpdated.Created_By = model.Created_By;
            //entityToUpdated.Created_Action = model.Created_Action;
        }
    }

    public class ProjectConverter : IModelConverter<VT_Projects, Project>
    {
        public Project Convert(VT_Projects entity)
        {
            Project model = new Project();
            model.ID = entity.Project_ID;
            model.Name = entity.Project_Name;
            model.Type = entity.Project_Type;
            model.Year = entity.Project_Year;
            model.Address = entity.Project_Address;
            model.Awards = entity.Project_Awards;
            model.Description = entity.Project_Description;
            //model.Image = new ProjectImage()
            //{
            //    ID = entity.Project_ID,
            //    Image = entity.ProjectImage.Project_Image
            //};
          
            return model;
        }

        public void ReverseConvert(Project model, VT_Projects entityToUpdated)
        {
            entityToUpdated.Project_Address = model.Address;
            entityToUpdated.Project_Type = model.Type;
            entityToUpdated.Project_Year = model.Year;
            entityToUpdated.Project_Name = model.Name;
            entityToUpdated.Project_Awards = model.Awards;
            entityToUpdated.Project_Description = model.Description;
            //entityToUpdated.ProjectImage = new VT_ProjectImage()
            //{
            //    Project_ID = entityToUpdated.Project_ID,
            //    Project_Image = model.Image.Image
            //};
        }
    }

    public class ProjectInfoConverter : IModelConverter<VT_Items, Project>
    {
        public Project Convert(VT_Items entity)
        {
            Project model = new Project();
            model.ID = entity.Project_ID;
            model.Name = entity.Project.Project_Name;
            model.Type = entity.Project.Project_Type;
            model.Year = entity.Project.Project_Year;
            model.Address = entity.Project.Project_Address;
            model.Awards = entity.Project.Project_Awards;
            model.Description = entity.Project.Project_Description;
            model.ViewCount = entity.View_Count;

            return model;
        }

        public void ReverseConvert(Project model, VT_Items entityToUpdated)
        {
            
        }
    }

    public class TicketConverter : IModelConverter<VT_Tickets, Ticket>
    {

        public Ticket Convert(VT_Tickets entity)
        {
            Ticket model = new Ticket();
            model.Activity_ID = entity.Activity_ID;
            model.Content = entity.Ticket_Content;
            model.ID = entity.Ticket_ID;
            model.No = entity.Ticket_No;
            model.Status = entity.Ticket_Status;
            model.SubmitTime = entity.Ticket_SubmitTime;
            model.User_ID = entity.User_ID;

            model.Created_Action = entity.Created_Action;
            model.Created_By = entity.Created_By;
            model.Created_Date = entity.Created_Date;

            model.Last_Update_Action = entity.Last_Update_Action;
            model.Last_Update_By = entity.Last_Update_By;
            model.Last_Update_Date = entity.Last_Update_Date;
            return model;
        }

        public void ReverseConvert(Ticket model, VT_Tickets entityToUpdated)
        {
            entityToUpdated.Ticket_Content = model.Content;
            entityToUpdated.Ticket_No = model.No;
            entityToUpdated.Ticket_Status = model.Status;
            entityToUpdated.Ticket_SubmitTime = model.SubmitTime;

            entityToUpdated.Last_Update_Date = model.Last_Update_Date;
            entityToUpdated.Last_Update_By = model.Last_Update_By;
            entityToUpdated.Last_Update_Action = model.Last_Update_Action;

            entityToUpdated.Created_Date = model.Created_Date;
            entityToUpdated.Created_By = model.Created_By;
            entityToUpdated.Created_Action = model.Created_Action;
        }
    }

    public class StatConverter : IModelConverter<VT_Items, Stat> 
    {
        public Stat Convert(VT_Items entity)
        {
            Stat model = new Stat();            
            model.QID = entity.Question_ID;
            model.QNO = entity.Question.Question_No.ToString();
            model.QTitle = entity.Question.Question_Title;
            model.Offline_Count = entity.Offline_Count;
            model.Online_Count = entity.Online_Count; 
            model.Title = entity.Item_Title;            
            return model;
        }

        public void ReverseConvert(Stat model, VT_Items entityToUpdated)
        {
            
        }
    }

    public class ProjectItemConverter : IModelConverter<VT_Items, ProjectItem>
    {
        public ProjectItem Convert(VT_Items entity)
        {
            ProjectItem model = new ProjectItem();            
            model.QID = entity.Question_ID;
            model.QNO = entity.Question.Question_No.ToString();
            model.QTitle = entity.Question.Question_Title;
            model.PID = entity.Project.Project_ID;
            model.pType = entity.Project.Project_Type;
            model.pName = entity.Project.Project_Name;
            if (!string.IsNullOrEmpty(entity.Project.Project_Year))
            {
                model.pYear = entity.Project.Project_Year;
                model.pYearGroup = entity.Project.Project_Year.Substring(0, 3) + "0";
            }
            model.Selected = false;
            return model;
        }

        public void ReverseConvert(ProjectItem model, VT_Items entityToUpdated)
        {

        }
    }

    #endregion
}